package com.liuyc.lib.ui.nestedScroll

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.annotation.AttrRes
import androidx.annotation.RequiresApi
import androidx.annotation.StyleRes
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.liuyc.lib.image.loader.ImageLoaderManager
import com.liuyc.lib.tools.ext.loadChildViewByParentView

/**
 * 此子类中必然包含一个 viewpager2  否则会抛出异常
 * viewPager2 内会再嵌套一个 recycleView
 */
class ChildNestedScrollViewPager2LinearLayout : ChildNestedScrollLinearLayout {

    private lateinit var mViewPager: ViewPager2


    constructor(context: Context) : super(context) {}
    constructor(
            context: Context, attrs: AttributeSet?
    ) : super(context, attrs) {
        initialize(context, attrs)
    }

    constructor(
            context: Context, attrs: AttributeSet?,
            @AttrRes defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        initialize(context, attrs)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(
            context: Context, attrs: AttributeSet?,
            @AttrRes defStyleAttr: Int, @StyleRes defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        initialize(context, attrs)
    }


    private fun initialize(context: Context, attrs: AttributeSet?) {
        //设定为 垂直布局
        orientation = LinearLayout.VERTICAL
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        loadViewPager()
    }


    /**
     * 获取 ViewPager2
     */
    private fun loadViewPager() {
        var viewPager = loadChildViewByParentView<ViewPager2>(this)

        if (viewPager == null) {
            throw Exception("ChildNestedScrollLinearLayout 中不包含指定的 ViewPager2")
        }

        mViewPager = viewPager

        mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                loadCurrentShowItemRecycleView(position)
            }
        })
    }

    private fun loadCurrentShowItemRecycleView(position: Int) {
        val childRecyclerView = loadChildViewByParentView<RecyclerView>(mViewPager)
        childRecyclerView?.let {
            var linearLayoutManager = childRecyclerView.layoutManager as LinearLayoutManager
            var itemView = linearLayoutManager.findViewByPosition(position)
            if (itemView != null){
                mChildRecycleView = loadChildNestedScrollRecycleView(itemView)
//                setOnScrollListener()
            }
        }
    }

    fun setOnScrollListener(){
        mChildRecycleView!!.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

                when(newState){
                    RecyclerView.SCROLL_STATE_IDLE ->{
                        ImageLoaderManager.getInstance().resumeLoadImage(context)
                    }
                    else ->{
                        ImageLoaderManager.getInstance().pauseLoadImage(context)
                    }
                }
            }
        })
    }

    private fun loadChildNestedScrollRecycleView(itemView: View): ChildNestedRecycleView? {
        if (itemView is ViewGroup) {
            for (i in 0 until itemView.childCount) {
                var childView = itemView.getChildAt(i)
                if (childView is ChildNestedRecycleView) {
                    return childView
                } else {
                    return loadChildNestedScrollRecycleView(childView)
                }
            }
        }
        return null
    }




}